﻿function Wave() {
}

Wave.prototype.init = function(id, game) {
    this.id = id;
    this.running = false;
    this.enemies = new Array();
	this.enemiesKilled = 0;
    this.timer = 0;
    this.launchedEnemies = 0; //Number of launched enemies which must be staggered for google directions to not crap out
    this.curCycle = 0;
    this.maxCycle = 100;
    this.game = game;
	
    //Set target based on weighted randomness
	if (game.bases["green"].health <= 0)
	    this.weightGreen = 0.0;
	if (game.bases["teal"].health <= 0)
	    this.weightTeal = 0.0;
	if (game.bases["blue"].health <= 0)
	    this.weightBlue = 0.0;
	
	var boundaries = new GLatLngBounds(this.waveCenter.transform(-.005, -.01), this.waveCenter.transform(.005, .01));
    this.portalOverlay = new GGroundOverlay("http://labs.ribaudio.com/herosquad/img/portal1.png", boundaries);
    this.smallPortalOverlay = new GGroundOverlay("http://labs.ribaudio.com/herosquad/img/portal1.png", boundaries);
    game.smallMap.addOverlay(this.smallPortalOverlay);
    game.map.addOverlay(this.portalOverlay);
	
	//Create zombies
    for (i = 0; i < this.constructor.numZombies; i++) {
        lat = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lat();
        lng = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lng();
        curZombie = new Zombie(new GLatLng(lat, lng), "zombie" + this.enemies.length);
        curZombie.onReachedTarget.subscribe(game.creatureOnReachedTarget, game);
        curZombie.onChangeIcon.subscribe(game.onChangeIcon, game);
        curZombie.onKilled.subscribe(game.onCreatureKilled, game);
        curZombie.onExploded.subscribe(game.onCreatureExploded, game);

        chanceBlue = Math.random() * this.weightBlue;
        chanceGreen = Math.random() * this.weightGreen;
        chanceTeal = Math.random() * this.weightTeal;
        
		if (chanceBlue >= chanceGreen && chanceBlue >= chanceTeal)
            curZombie._targetPoint = game.bases["blue"].point;
        else if (chanceGreen >= chanceTeal && chanceGreen >= chanceBlue)
            curZombie._targetPoint = game.bases["green"].point;
        else if (chanceTeal >= chanceGreen && chanceTeal >= chanceBlue)
            curZombie._targetPoint = game.bases["teal"].point;

        this.enemies.push(curZombie);
    }
	
	//Create demons
    for(i=0;i<this.constructor.numDemons;i++)
    {
        lat = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lat();
        lng = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lng();
        curDemon = new Demon(new GLatLng(lat, lng), "demon" + this.enemies.length);
        curDemon.onReachedTarget.subscribe(game.creatureOnReachedTarget, game);
        curDemon.onChangeIcon.subscribe(game.onChangeIcon, game);
        curDemon.onKilled.subscribe(game.onCreatureKilled, game);
        curDemon.onExploded.subscribe(game.onCreatureExploded, game);
        
        chanceBlue = Math.random() * this.weightBlue;
        chanceGreen = Math.random() * this.weightGreen;
        chanceTeal = Math.random() * this.weightTeal;
		
        if (chanceBlue >= chanceGreen && chanceBlue >= chanceTeal)
            curDemon._targetPoint = game.bases["blue"].point;
        else if (chanceGreen >= chanceTeal && chanceGreen >= chanceBlue)
            curDemon._targetPoint = game.bases["green"].point;
        else if (chanceTeal >= chanceGreen && chanceTeal >= chanceBlue)
            curDemon._targetPoint = game.bases["teal"].point;
           
        this.enemies.push(curDemon);
    }
	
	//Create lost souls
    for (i = 0; i < this.constructor.numWasps; i++) {
        lat = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lat();
        lng = Math.random() * (this.constructor.maxRangeFromCenter * 2.0) - this.constructor.maxRangeFromCenter + this.waveCenter.lng();
        curWasp = new Wasp(new GLatLng(lat, lng), "lostsoul" + this.enemies.length);
        curWasp.onReachedTarget.subscribe(game.creatureOnReachedTarget, game);
        curWasp.onChangeIcon.subscribe(game.onChangeIcon, game);
        curWasp.onKilled.subscribe(game.onCreatureKilled, game);
        curWasp.onExploded.subscribe(game.onCreatureExploded, game);

        chanceBlue = Math.random() * this.weightBlue;
        chanceGreen = Math.random() * this.weightGreen;
        chanceTeal = Math.random() * this.weightTeal;
        
		if (chanceBlue >= chanceGreen && chanceBlue >= chanceTeal)
            curWasp._targetPoint = game.bases["blue"].point;
        else if (chanceGreen >= chanceTeal && chanceGreen >= chanceBlue)
            curWasp._targetPoint = game.bases["green"].point;
        else if (chanceTeal >= chanceGreen && chanceTeal >= chanceBlue)
            curWasp._targetPoint = game.bases["teal"].point;

        this.enemies.push(curWasp);
    }

    //Randomly sort the array of enemies
    this.enemies = this.enemies.sort(function(a, b) { if (Math.random() > 0.5) return 0; else return 1; });
}

//Static properties
Wave.maxWaves = 4;
//Static methods
Wave.getWaveById = function(id, game) {
	$("#roundNum").text(id);
    switch (id) {
        case 1: return new Wave1(game);
        case 2: return new Wave2(game);
        case 3: return new Wave3(game);
		case 4: return new Wave4(game);
    }
}

Wave.prototype.run = function() {
    this.curCycle++;

    //Increment the number of allowable launched enemies depending on the speed of demployment set by the wave
    if (this.curCycle % this.constructor.launchSpeed == 0 && this.launchedEnemies < this.enemies.length) {
        
		//Only add 1/5 enemies to the smallmap
		if(this.launchedEnemies % 5 == 0)
			game.smallMap.addOverlay(this.enemies[this.launchedEnemies].smallMarker);
		else
			this.enemies[this.launchedEnemies].smallMarker = null;
        this.launchedEnemies++;
    }

    //Only run enemies that have been launched
    for (i = 0; i < this.launchedEnemies; i++) {
        if (this.enemies[i]) {
            if (!this.enemies[i].isDead()) {
                this.enemies[i].run();
				
                //Once they enter the screen they are added
                if (this.enemies[i].onMap == false)
                    if (this.game.map.getBounds().containsLatLng(this.enemies[i]._curPoint)) {
	                    this.game.map.addOverlay(this.enemies[i].marker);
	                    this.enemies[i].onMap = true;
                }
            }
            else {
                delete this.enemies[i];
				this.enemiesKilled++;
            }
        }
    }

    if (this.curCycle >= this.maxCycle) this.curCycle = 0;
}

//Returns false if there are any monsters left alive.  True if all are dead;
Wave.prototype.isComplete = function() {
    return this.enemiesKilled >= this.enemies.length;
}

